
/**
 * 把一个reducers对象变成一个reducer函数
 * @param {*} reducers 
 */
function combineReducers(reducers) {
    let rootReducer = function(state = {}, action) {
        let nextState = {};
        for (let key in reducers) {
            // 分reducer
            const reducer = reducers[key];
            // 老的分状态
            const previousStateForKey = state[key];
            // 计算新的状态
            const nextStateForKey = reducer(previousStateForKey, action);
            // 新状态保存到nextState
            nextState[key] = nextStateForKey;
        }
        return nextState;
    }
    return rootReducer;
}

export default combineReducers;
